package monitor

import (
	"fmt"
	"gluenet/pkg/apis"
	"gluenet/pkg/meta"
	"time"
)

func MetricsHistoryMethod(c *apis.GlueContext) {
	startTime, _ := c.WebContext.URLParamInt64("start")
	endTime, _ := c.WebContext.URLParamInt64("end")
	lookBack, _ := c.WebContext.URLParamInt64("lookback")
	platform := c.WebContext.URLParam("platform")
	metric := c.WebContext.URLParam("metric")
	guid := c.WebContext.URLParam("guid")
	object := c.WebContext.URLParam("object")

	method := c.WebContext.URLParam("method")

	if lookBack != 0 {
		endTime = time.Now().Unix()
		startTime = endTime - lookBack + 1
	}
	size := endTime - startTime + 1
	if size <= 0 {
		c.Logger.Errorf("error : size less < than 0")
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("error : size less < than"), nil))
		return
	}

	var data *meta.Metrics
	if err := c.DataBase.MetricsMethodQuery(object, platform, metric, guid, method, startTime, endTime, &data); err != nil {
		c.Logger.Errorf("query container cpus error: %v", err)
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query container cpus error %v", err), nil))
		return
	}

	ms := data.Metrics.([]map[string]interface{})
	if len(ms) == 0 {
		c.WebContext.JSON(apis.ResponseOK(nil))
		return
	}

	var key, value string
	switch object {
	case apis.ObjectApplication:
		key = "container"
		value = ms[0]["container"].(string)
	case apis.ObjectMachine:
		key = "hostname"
		value = ms[0]["hostname"].(string)
	}

	c.WebContext.JSON(apis.ResponseOK(map[string]interface{}{
		key:       value,
		"metrics": data.Metrics,
	}))
}

func MetricsHistorySum(c *apis.GlueContext) {
	startTime, _ := c.WebContext.URLParamInt64("start")
	endTime, _ := c.WebContext.URLParamInt64("end")
	lookBack, _ := c.WebContext.URLParamInt64("lookback")
	platform := c.WebContext.URLParam("platform")
	metric := c.WebContext.URLParam("metric")
	guid := c.WebContext.URLParam("guid")
	childs := c.WebContext.URLParam("childs")
	object := c.WebContext.URLParam("object")

	if lookBack != 0 {
		endTime = time.Now().Unix()
		startTime = endTime - lookBack + 1
	}
	size := endTime - startTime + 1
	if size <= 0 {
		c.Logger.Errorf("error : size less < than 0")
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("error : size less < than"), nil))
		return
	}

	var data *meta.Metrics
	if err := c.DataBase.MetricsSumQuery(object, platform, metric, guid, childs, startTime, endTime, &data); err != nil {
		c.Logger.Errorf("query container cpus error: %v", err)
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query container cpus error %v", err), nil))
		return
	}

	ms := data.Metrics.([]map[string]interface{})
	if len(ms) == 0 {
		c.WebContext.JSON(apis.ResponseOK(nil))
		return
	}

	var key, value string
	switch object {
	case apis.ObjectApplication:
		key = "container"
		value = ms[0]["container"].(string)
	case apis.ObjectMachine:
		key = "hostname"
		value = ms[0]["hostname"].(string)
	}

	c.WebContext.JSON(apis.ResponseOK(map[string]interface{}{
		key:       value,
		"metrics": data.Metrics,
	}))
}

func Metrics(c *apis.GlueContext) {
	startTime, _ := c.WebContext.URLParamInt64("start")
	endTime, _ := c.WebContext.URLParamInt64("end")
	lookBack, _ := c.WebContext.URLParamInt64("lookback")
	platform := c.WebContext.URLParam("platform")
	metric := c.WebContext.URLParam("metric")
	guid := c.WebContext.URLParam("guid")
	child := c.WebContext.URLParam("childs")
	object := c.WebContext.URLParam("object")

	//c.Logger.Debugf("%v %v %v", startTime, endTime, lookBack)

	if lookBack != 0 {
		endTime = time.Now().Unix()
		startTime = endTime - lookBack + 1
	}
	size := endTime - startTime + 1
	c.Logger.Debugf("%v %v %v", startTime, endTime, lookBack)
	if size <= 0 {
		c.Logger.Errorf("error : size less < than 0")
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("error : size less < than"), nil))
		return
	}

	var data *meta.Metrics
	if err := c.DataBase.MetricsQuery(object, platform, metric, guid, child, startTime, endTime, &data); err != nil {
		c.Logger.Errorf("query container cpus error: %v", err)
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query container cpus error %v", err), nil))
		return
	}

	c.WebContext.JSON(apis.ResponseOK(data.Metrics))
}

func MetricsList(c *apis.GlueContext) {
	var data *meta.Metrics
	if err := c.DataBase.MetricsList(&data); err != nil {
		c.Logger.Errorf("query container cpus error: %v", err)
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query container cpus error %v", err), nil))
		return
	}

	c.WebContext.JSON(apis.ResponseOK(data.Metrics))
}
